package com.alibaba.csp.sentinel.demo.spring.web;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import com.alibaba.csp.sentinel.slots.block.degrade.circuitbreaker.CircuitBreakerStrategy;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.csp.sentinel.slots.system.SystemRule;
import com.alibaba.csp.sentinel.slots.system.SystemRuleManager;

public class SentinelRuleDefine {
	
	 public static String KEY="ERROR_RATIO";
	 static void defineRule() {
		 initDegradeRule();
		 initDegradeRuleInterceptor();
		 //initFlowQpsRule();
		 //initSystemRule();
	 }

	public static void initDegradeRule() {
	        List<DegradeRule> rules = new ArrayList<>();
	        DegradeRule rule = new DegradeRule(KEY)
	            .setGrade(CircuitBreakerStrategy.ERROR_COUNT.getType())
	            // Set ratio threshold to 50%.
	            .setCount(1)
	            .setStatIntervalMs(60000)
	            .setSlowRatioThreshold(0.1)
	            .setMinRequestAmount(3)
	            // Retry timeout (in second)
	            .setTimeWindow(30);
	        rules.add(rule);
	        DegradeRuleManager.loadRules(rules);
	        System.out.println("Degrade rule loaded: " + rules);
	      
	        
	    }
	 
	public static void initDegradeRuleInterceptor() {
        List<DegradeRule> rules = new ArrayList<>();
        //use pattern name as resource
        DegradeRule rule = new DegradeRule("/Interceptor")
            .setGrade(CircuitBreakerStrategy.ERROR_COUNT.getType())
            // Set ratio threshold to 50%.
            .setCount(1)
            .setStatIntervalMs(60000)
            .setSlowRatioThreshold(0.1)
            .setMinRequestAmount(3)
            // Retry timeout (in second)
            .setTimeWindow(30);
        
        rules.add(rule);
        DegradeRuleManager.loadRules(rules);
        System.out.println("Degrade rule loaded: " + rules);
      
        
    }
	
	private static void initSystemRule() {
        List<SystemRule> rules = new ArrayList<SystemRule>();
        SystemRule rule = new SystemRule();
        // max load is 3
        rule.setHighestSystemLoad(3.0);
        // max cpu usage is 60%
        rule.setHighestCpuUsage(0.06);
        // max avg rt of all request is 10 ms
        rule.setAvgRt(1);
        // max total qps is 20
        rule.setQps(1);
        // max parallel working thread is 10
        rule.setMaxThread(1);
        //rule.setLimitApp(KEY);
        
        rule.setResource(KEY+"notexist");
        rules.add(rule);
        SystemRuleManager.loadRules(Collections.singletonList(rule));
    }
 
	  private static void initFlowQpsRule() {
	        List<FlowRule> rules = new ArrayList<FlowRule>();
	        FlowRule rule1 = new FlowRule();
	        rule1.setResource(KEY);
	        // set limit qps to 20
	        rule1.setCount(1);
	        rule1.setGrade(RuleConstant.FLOW_GRADE_QPS);
	        rule1.setLimitApp("default");
	        rules.add(rule1);
	        FlowRuleManager.loadRules(rules);
	    }
	  
}
